package com.lance.answer.gui.answer.gk.answer.lms;

import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.Objects;

public class DoAnswerTestForDrop {


    public static void main(String[] args) throws Exception {

        String driverPath = "d:\\ynouAutoStudy\\chromedriver.exe";
        ChromeDriver driver = Util.getDriver(driverPath);
        String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
        driver.get(url);
        Thread.sleep(3000);

        String userName = "2165001416843";
        String pwd = "Ouchn@2021";
        LmsUtil.login(driver, userName, pwd);


        String doExamUrl = "https://lms.ouchn.cn/course/10000003755/learning-activity/full-screen#/exam/10000056153";

        driver.get(doExamUrl);

        Util.switchToLast(driver);
        Thread.sleep(3000);

        // 确认开始答题
       confirmDoAnswer(driver);

        // 找到所有的问题element
        Thread.sleep(5000);

        WebElement matchingAnswerBox = driver.findElement((By.cssSelector("div[class='matching-answer-box']")));

        List<WebElement> dragAreaList = matchingAnswerBox.findElements(By.cssSelector("div[class^='clone-area drag-area ng-scope']"));
        //    List<WebElement> dragAreaList = matchingAnswerBox.findElements(By.cssSelector("div[class='content-wrapper']"));
        //   List<WebElement> dragAreaList = matchingAnswerBox.findElements(By.cssSelector("div[class='content-center']"));
        //  List<WebElement> dragAreaList = matchingAnswerBox.findElements(By.cssSelector("div[class='icon icon-drag']"));
        System.err.println("dragAreaList:" + dragAreaList.size());
        List<WebElement> targetAreaList = matchingAnswerBox.findElements(By.cssSelector("div[class='list-panel option ng-isolate-scope']"));
        // List<WebElement> targetAreaList = matchingAnswerBox.findElements(By.cssSelector("li[ng-repeat='subSubject in subject.sub_subjects']"));
        System.err.println("targetAreaList:" + targetAreaList.size());

        Actions action = new Actions(driver);


        try {
            WebElement source = dragAreaList.get(0);
            WebElement target = targetAreaList.get(0);

            System.err.println("????1111:" + source.getAttribute("draggable"));

            System.err.println("source:x" + source.getLocation().getY() + " y:" + source.getLocation().getY());
            System.err.println("target:x" + target.getLocation().getY() + " y:" + target.getLocation().getY());


            mouseHoverByJavaScript(target,driver);
            Thread.sleep(2000);
            action.moveToElement(target).perform();
            Thread.sleep(1000);
            action.doubleClick(target).perform();
            Thread.sleep(1000);
            action.doubleClick(target).perform();
            Thread.sleep(1000);

            action.moveToElement(source).perform();
            Thread.sleep(1000);
            action.doubleClick(source).perform();
            Thread.sleep(1000);
            action.clickAndHold(source).perform();
            Thread.sleep(1000);


//              action.clickAndHold(source);
//              Thread.sleep(1000);
            //      action.clickAndHold(source).perform();

            //   action.dragAndDrop(source, target).build().perform();


            // action.dragAndDrop(source, target).perform();



            System.err.println("????222:" + source.getAttribute("draggable"));
            //    action.dragAndDropBy(source, 10,10).build().perform();

          //  action.dragAndDrop(source, target).perform();
            action.dragAndDrop(source, target).build().perform();

       //     target.sendKeys("11111111111111");


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void mouseHoverByJavaScript(WebElement targetElement, WebDriver driver)

    {

        String mouseHoverjs = "var evObj = document.createEvent('MouseEvents');" +

                "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +

                "arguments[0].dispatchEvent(evObj);";

        JavascriptExecutor js = (JavascriptExecutor)driver;

        js.executeScript(mouseHoverjs, targetElement);

    }

    public static void confirmDoAnswer(WebDriver driver) throws InterruptedException {

        //  点击开始答题
        driver.findElement(By.cssSelector("div[class='exam-button-container ng-scope']")).findElement(By.tagName("span")).click();
        Thread.sleep(3000);
        //  for = confirm 点击勾选 我已知晓考试要求并承诺遵守考试纪律 ng-valid ng-touched ng-dirty ng-valid-parse ng-empty
        try {
            new WebDriverWait(driver, Duration.ofSeconds(30)).until((ExpectedCondition<WebElement>)
                    d -> Objects.requireNonNull(d).findElement(By.cssSelector("input[name='confirm']"))).click();
        } catch (Exception e) {

        }
        Thread.sleep(3000);

        for (WebElement startAnswerWe : new WebDriverWait(driver, Duration.ofSeconds(20)).until((ExpectedCondition<List<WebElement>>)
                d -> Objects.requireNonNull(d).findElements(By.cssSelector("button[class='button button-green medium ng-binding']")))) {
            // button button-green medium ng-binding
            if ("开始答题".equals(startAnswerWe.getText()) || "继续答题".equals(startAnswerWe.getText())) {
                startAnswerWe.click();
                break;
            }

        }
    }

}




